//
//  LGSymptomDetailView.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/10/12.
//

#import "LGSymptomDetailView.h"

@interface LGSymptomDetailView()<UITableViewDelegate,UITableViewDataSource>

/**   */
@property (nonatomic , strong)UIView *baseView;
/**   */
@property (nonatomic , strong)UILabel *titleLabel;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)UIButton *sureBtn;



@end

@implementation LGSymptomDetailView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sureButtonAction)]];
        [self addSubview:self.baseView];
        [self.baseView addSubview:self.titleLabel];
        [self.baseView addSubview:self.tableView];
        [self.baseView addSubview:self.sureBtn];
        [UIView animateWithDuration:0.4 animations:^{
            self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0.5];
            self.baseView.frame = CGRectMake(0, Screen_H-viewPix(445), Screen_W, viewPix(460));
        }];
    }
    return self;
}

-(void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    [self.tableView reloadData];
}

-(void)sureButtonAction{
    [UIView animateWithDuration:0.4 animations:^{
        self.backgroundColor = [UIColor colorWithString:@"#000000" alpha:0];
        self.baseView.frame = CGRectMake(0, Screen_H, Screen_W, viewPix(460));
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSArray *keyArray = @[@"symptom_name",@"start_date",@"end_date",@"days"];
    NSArray *unitArray = @[@"",@"",@"",@"天"];
    LGSymptomDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.content = [NSString stringWithFormat:@"%@%@",self.dataDic[keyArray[indexPath.section]],unitArray[indexPath.section]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return viewPix(30);
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(30))];
    baseView.backgroundColor = [UIColor whiteColor];
    NSArray *titleArray = @[@"症状名称",@"出现时间",@"结束时间",@"持续时间"];
    UIButton *titleBtn = [UIButton buttonWithTitle:titleArray[section] titleFont:LGFont(16) textColor:@"#666666" imageName:[NSString stringWithFormat:@"症状详情-%@",titleArray[section]]];
    titleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    titleBtn.frame = CGRectMake(viewPix(20), 0, viewPix(200), viewPix(30));
    titleBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 5, 0, 0);
    [baseView addSubview:titleBtn];
    return baseView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.01)];
}

#pragma mark
#pragma mark ====> 创建控件
-(UIView *)baseView{
    if(!_baseView){
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, Screen_H, Screen_W, viewPix(460))];
        _baseView.backgroundColor = [UIColor whiteColor];
        _baseView.cornerRidus = 15;
    }
    return _baseView;
}

-(UILabel *)titleLabel{
    if(!_titleLabel){
        _titleLabel = [UILabel lableWithText:@"症状详情" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentCenter lines:1];
        _titleLabel.frame = CGRectMake(0, viewPix(24), Screen_W, viewPix(22));
    }
    return _titleLabel;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, viewPix(60), Screen_W, viewPix(313)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGSymptomDetailCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
    }
    return _tableView;
}

-(UIButton *)sureBtn{
    if(!_sureBtn){
        _sureBtn = [UIButton buttonWithTitle:@"确定" topMargin:kMaxY(self.tableView.frame)+viewPix(8)];
        [_sureBtn addTarget:self action:@selector(sureButtonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sureBtn;
}

@end


#pragma mark
#pragma mark ====> LGSymptomDetailCell

@interface LGSymptomDetailCell()

/**   */
@property (nonatomic , strong)UILabel *contentLabel;

@end

@implementation LGSymptomDetailCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.contentLabel];
        [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView).offset(viewPix(10));
            make.left.equalTo(self.contentView).offset(viewPix(40));
            make.right.equalTo(self.contentView).offset(-viewPix(15));
            make.bottom.equalTo(self.contentView).offset(-viewPix(15));
        }];
    }
    return  self;
}

-(void)setContent:(NSString *)content{
    _content = content;
    self.contentLabel.text = content;
}

-(UILabel *)contentLabel{
    if(!_contentLabel){
        _contentLabel = [UILabel lableWithText:@"" colorString:@"#333333" textFont:LGFontWeight(16, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:0];
    }
    return _contentLabel;
}

@end
